<template>
	<view class="p-2 bg-light content">
		<!-- 买方需求商品详情 -->
		<view class="bg-white my-2 p-2 cont">
			<text class="h3 font-weight-bold">订单信息</text>
			<uni-table border stripe emptyText="暂无更多数据">
				<uni-tr>
					<uni-th align="center">商品图片</uni-th>
					<uni-th align="center">商品名称</uni-th>
					<uni-th align="center">商品单价</uni-th>
					<uni-th align="center">商品数量</uni-th>
					<uni-th align="center">总金额</uni-th>
				</uni-tr>
				<uni-tr v-for="row, index in sales_order.Children.sales_order_detail" :key="index">
					<uni-td align="center">
						<image style="width: 100rpx;height: 100rpx;" v-if="row.Article_URL != undefined"
							:src="Domain_IMG + row.Article_URL"></image>
					</uni-td>
					<uni-td align="center">{{ row.Article_Name }}</uni-td>
					<uni-td align="center">￥{{ row.Sales_Unit_Price }}</uni-td>
					<uni-th align="center">{{ row.Quantity }}</uni-th>
					<uni-th align="center">￥{{ Number(row.Sales_Unit_Price) * Number(row.Quantity) }}</uni-th>
				</uni-tr>
			</uni-table>
		</view>
		<!-- 买方信息 -->
		<view class="bg-white my-2 p-2 cont">
			<text class="h3 font-weight-bold">提货人信息</text>
			<view class="p-2">
				<uni-forms :modelValue="form" ref="formData">
					<uni-forms-item label-width="100" name="Buyer_Contact" label="提货人姓:">
						<uni-easyinput v-model="sales_order.Sales_Contacts" type="text" placeholder="请输入提货人姓名" />
					</uni-forms-item>
					<uni-forms-item label-width="100" name="Buyer_Contact_Phone" label="手机号:">
						<uni-easyinput v-model="sales_order.Sales_Phone" type="text" placeholder="请输入提货人手机号" />
					</uni-forms-item>
					<uni-forms-item label-width="100" label="住址:">
						<uni-data-picker  v-if="Address.length != 0" style="width: 500rpx;"
							v-model="address" :map="{ text: 'text', value: 'value' }" :localdata="Address"
							placeholder="请选择地址"></uni-data-picker>
					</uni-forms-item>
					<uni-forms-item label-width="100" name="Buyer_Contact_Address" label="详情地址:">
						<uni-easyinput v-model="sales_order.Sales_Address" type="text" placeholder="请输入提货人详细地址" />
					</uni-forms-item>
				</uni-forms>
			</view>
		</view>
		<!-- 提交订单 -->
		<view class="bg-white p-2">
			<button :loading="loading" class="uni-button" type="primary" size="default" @click="Pay">提交订单</button>
		</view>
	</view>
</template>

<script>
import Tradingchat from "@/components/Tradingchat2/Tradingchat.vue";
import {
	WX2CheckStockAction
} from '@/api/WX.js'
import {
	getAddAction
} from '@/api/global.js'
import {
	GroupWeChatNativeAction
} from '@/api/group.js'
import times from "@/mixins/time.js";
const app = getApp();
const {
	Address
} = data222();
import {
	data222
} from "@/static/js/data.js";
export default {
	onLoad(e) {
		console.log(uni.getStorageSync('GoodsDetail'))
		this.sales_order.Store_SerialNumber = e.Store_SerialNumber
		this.sales_order.Article_SerialNumber = e.Article_SerialNumber
		this.sales_order.Children.sales_order_detail[0].Article_Specifications_SerialNumber = e
			.Article_Specifications_SerialNumber
		this.sales_order.Children.sales_order_detail[0].Article_URL = uni.getStorageSync('GoodsDetail').Picture_URL
		this.sales_order.Sales_Amount = this.accMul(uni.getStorageSync('Goodsprice'), uni.getStorageSync('Quantity'))
		this.sales_order.Children.sales_order_detail[0].Sales_Unit_Price = uni.getStorageSync('Goodsprice')
		this.sales_order.Children.sales_order_detail[0].Article_SerialNumber = e.Article_SerialNumber
		this.sales_order.Children.sales_order_detail[0].Quantity = uni.getStorageSync('Quantity')
		this.sales_order.Children.sales_order_detail[0].Amount = this.accMul(uni.getStorageSync('Goodsprice'), uni
			.getStorageSync('Quantity'))
		this.sales_order.Children.sales_order_detail[0].Sales_Unit_Price_Stock_SerialNumber = e
			.Sales_Unit_Price_Stock_SerialNumber
		this.sales_order.Children.sales_order_detail[0].Unit_Price = uni.getStorageSync('GoodsDetail').Unit_Price
		this.sales_order.Children.sales_order_detail[0].Article_Name = uni.getStorageSync('GoodsDetail').Article_Name
		console.log(this.sales_order)
	},
	components: {
		Tradingchat,
	},
	data() {
		return {
			loading: false,
			radioValue: 1,
			show: false,
			pay: false,
			payURL: "",
			screenWidth: uni.getSystemInfoSync().windowWidth,
			ThemeColors: app.globalData.ThemeColors,
			Domain_IMG: app.globalData.Domain_IMG,
			Domain_Business: app.globalData.Domain_Business,
			Address: Address(),
			form: {
				name: '',
				phone: '',
				address: '',
				addressDetail: ''
			},
			address: [],
			sales_order: {
				Sales_Order_SerialNumber: '',
				Store_SerialNumber: '',
				Sales_Date: times.nowtime(),
				Open_ID: uni.getStorageSync('use'),
				Sales_Amount: '',
				Sales_Contacts: uni.getStorageSync('username'),
				Sales_Phone: uni.getStorageSync('use'),
				Sales_Address: '',
				Sales_Message: '',
				Collection_Amount: 0,
				Sales_ID: 'xiaoshou1',
				Receive_Type: 0,
				Refund_Type: 0,
				App_ID: 'wxd96a1f0fbc125263',
				Enterprise_Info_SerialNumber: uni.getStorageSync('enterprise'),
				Children: {
					sales_order_detail: [{
						Sales_Order_Detail_SerialNumber: '',
						Sales_Order_SerialNumber: '',
						Sales_Unit_Price_Stock_SerialNumber: '',
						Article_SerialNumber: '',
						Article_Name: '',
						Sign: '无货货主',
						Quantity: '',
						Unit_Price: '',
						Sales_Unit_Price: '',
						Amount: '',
						Receive_Type: 0,
						Refund_Type: 0,
						Enterprise_Info_SerialNumber: uni.getStorageSync('enterprise'),
						Article_Specifications_SerialNumber: '',
						Article_URL: '',
					}]
				}
			},
			rules: {
				Buyer_Contact: {
					rules: [{
						required: true,
						errorMessage: "请输入买方姓名",
					},
					{
						minLength: 1,
						maxLength: 5,
						errorMessage: "姓名长度在 {minLength} 到 {maxLength} 个字符",
					},
					],
				},
				Buyer_Contact_Phone: {
					rules: [{
						required: true,
						errorMessage: "请输入卖方手机号",
					},
					{
						validateFunction: function (rule, value, data, callback) {
							let iphoneReg = /^1[3-9]\d{9}$/
							if (!iphoneReg.test(value)) {
								callback('手机号码格式不正确，请重新填写')
							}
							return true
						}
					}
					]
				},
				Buyer_Contact_Address: {
					rules: [{
						required: true,
						errorMessage: "请输入详细地址",
					},],
				},
				Payment_SerialNumber: {
					rules: [{
						required: true,
						errorMessage: "请选择打款方式",
					},],
				},
				Transport_SerialNumber: {
					rules: [{
						required: true,
						errorMessage: "请选择运输方式",
					},],
				},
				Shipment_SerialNumber: {
					rules: [{
						required: true,
						errorMessage: "请选择装运方式",
					},],
				},
			},
			//测试
			Address: Address(),
			address: [],
			addressDetail: '',
			type: 'center',
			msgType: 'success',
			messageText: '这是一条成功提示',
			value: '',
		};
	},
	onReady() {
		this.$refs.formData.setRules(this.rules)
	},
	methods: {
		handleTabs(url) {
			uni.navigateTo({
				url
			})
		},
		messageToggle(error) {
			this.msgType = error
			this.messageText = `${error}`
			this.$refs.formData.open()
		},
		async Pay() {
			// if (this.sales_order.Sales_Phone == '') {
			// 	this.messageToggle('请填写手机号');
			// 	return
			// }
			// if (this.sales_order.Sales_Contacts == '') {
			// 	this.messageToggle('请填写联系人');
			// 	return
			// }
			// if (this.address.length == 0) {
			// 	this.messageToggle('请填写收货地址');
			// 	return
			// }
			// if (this.sales_order.Sales_Address == '') {
			// 	this.messageToggle('请填写详细的收货地址');
			// 	return
			// }
			uni.showLoading({
				title: '提交中...'
			})
			this.loading = true
			const res = await WX2CheckStockAction({
				data: {
					Data: JSON.stringify([{
						Sales_Unit_Price_Stock_SerialNumber: this.sales_order.Children
							.sales_order_detail[0].Sales_Unit_Price_Stock_SerialNumber,
						Quantity: this.sales_order.Children.sales_order_detail[0].Quantity,
					}])
				}
			})
			if (res.redirect && res.redirect == 'REDIRECT') {
				uni.navigateTo({
					url: '/pages/My/Login'
				})
			} else if (res.Status == 'insufficient') {
				this.loading = false
				uni.hideLoading()
				console.log('该商品暂时库存不足,无法购买');
				// Message({
				// 	type: 'error',
				// 	message: '该商品暂时库存不足,无法购买'
				// })
				return
			} else if (res.Status == 'success') {
				console.log(res)
				this.sales_order.Sales_Address = this.address[0] + '/' + this.address[1] + '/' + this.address[2] + '/' + this.sales_order.Sales_Address
				let data = {
					Data: JSON.stringify({
						sales_order: [this.sales_order]
					}),
					token: uni.getStorageSync('token'),
				}
				this.getAdd(data)
			}
		},
		async getAdd(data) {
			const res = await getAddAction({
				data
			})
			console.log(res)
			if (res.redirect && res.redirect == 'REDIRECT') {
				uni.navigateTo({
					url: '/pages/My/Login'
				})
			} else if (res.Status == 'success') {
				this.PaySrc(res.Data)
			}
		},
		async PaySrc(Sales_Order_SerialNumber) {
			let data = {
				Data: Sales_Order_SerialNumber,
				Collection_Type: '微信支付',
				token: uni.getStorageSync('token'),
				Order_Type: '1'
			}
			const res = await GroupWeChatNativeAction({
				data
			})
			console.log(res)
			if (res.redirect && res.redirect == 'REDIRECT') {
				uni.navigateTo({
					url: '/pages/login/login'
				})
			} else if (res.result_msg == 'SUCCESS') {
				this.loading = false
				uni.hideLoading()
				this.payURL = res.code_url
				this.pay = true
			} else {
				this.loading = false
				uni.hideLoading()
				console.log('提交失败,请稍后重试...');
				// Message({
				// 	type: 'error',
				// 	message: '提交失败,请稍后重试...'
				// })
			}
		},
		shows(e) {
			this.pay = e
		},
		accMul(arg1, arg2) { //浮点型相乘
			let m = 0,
				s1 = arg1.toString(),
				s2 = arg2.toString();
			try {
				m += s1.split(".")[1].length
			} catch (e) { }
			try {
				m += s2.split(".")[1].length
			} catch (e) { }
			return Number(s1.replace(".", "")) * Number(s2.replace(".", "")) / Math.pow(10, m)
		},
	},
};
</script>

<style lang="scss">
.content {
	padding: 20rpx;
	box-sizing: border-box;

	.cont {
		border-radius: 20rpx;
		background-color: #fff;
		padding: 25rpx;
		box-sizing: border-box;
		margin: 0 0 30rpx 0;

		.title1 {
			font-size: 35rpx;
			padding: 20rpx;
			box-sizing: border-box;

			view {
				margin: 20rpx 0;
			}

			.I {
				width: 70%;
				display: inline-block;
				margin: 0 0 0 10rpx;
			}
		}
	}
}
</style>